package org.brisling.login.queryService;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.SimpleResponseObj;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.exception.SessionExpiredException;
import org.brisling.common.exception.UserameOrPasswordWrongException;
import org.brisling.honest.domain.HonestDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class QueryLoginService extends AbstractBaseQueryService<HonestDomain>{
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	
	@RequestMapping("/{modulename}")	
	public ModelAndView getLoginPage(
			
						@PathVariable("modulename") String modulename,
						@RequestParam(value="userid",required=false) String userid
						
						) throws UserameOrPasswordWrongException, SessionExpiredException{
		if(modulename==null){
			modulename = "honest";
		}
		
//		String orgcode = SysConfig.getConfig("orgcode");
//		
//		String sitepath = null;
//		if(orgcode!=null && orgcode.trim().length()>0){
//			sitepath = String.format("/%1$s/%2$s/login", orgcode,modulename);
//		}else{
//			sitepath = String.format("/%1$s/login",modulename);
//		}
		

		String sitepath = String.format("/%1$s/login",modulename);
		sitepath = userid==null?sitepath:sitepath+"?loginname="+userid+"&modulename="+modulename;
		ModelAndView views = new ModelAndView(sitepath);
		return views;
		
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public SimpleResponseObj moduleLogin(@RequestParam(value="loginname",required=false) String username,
			@RequestParam(value="password",required=false) String password,
			@RequestParam(value="site",required=false) String site,
			@RequestParam(value="logintype",required=false) String logintype,
			@RequestParam(value="remenberme",required=false) Boolean isRememberMe,						
			@RequestBody(required=false) String body,HttpSession session,
			HttpServletRequest request,
			HttpServletResponse response) throws UserameOrPasswordWrongException, SessionExpiredException{
		if(site==null){
			site = "risk";
		}	
		if(logintype!=null && logintype.trim().compareTo("sso")==0){
			return shiroSecurityService.ssoLogin(username, site, session, request, response);
		}
		return shiroSecurityService.sysLogin(username, password, site,logintype, isRememberMe, body, session, request, response);
		
	}
	
	@RequestMapping("/{sitename}/main")	
	public ModelAndView getMainPage(@PathVariable("sitename") String sitename){
//		String orgcode = SysConfig.getConfig("orgcode");
		
		String sitepath = null;
//		if(orgcode!=null && orgcode.trim().length()>0){
//			sitepath = String.format("/%1$s/main", orgcode);
//		}else{
//			sitepath = "/main";
//		}
		
		sitepath = String.format("/%1$s/main", sitename);
		return new ModelAndView(sitepath);
	}
	
	
}
